package com.mfnote.view.setting;


import com.mfnote.view.R;
import com.mfnote.view.R.id;
import com.mfnote.view.R.layout;
import com.mfnote.view.R.style;
import com.mfnote.view.main.MainActivity;
import com.mfnote.view.other.MyApplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/** 此界面只有在启用加密后，再次进入界面时显示出来 **/
public class OpenEncyptActivity extends Activity {

	private String password; // 从配置文件中读取的数据
	private String savepassword; // 将要保存的密码

	private static boolean isEncypt = false;
	
	Button btn_openencyptDialog; //弹出输入框
	
	Dialog cancelEncryptDlg;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.openencypt);

		MyApplication.getInstance().addActivity(this);
		
		btn_openencyptDialog = (Button) this.findViewById(R.id.btn_openencypt_dialog);
		btn_openencyptDialog.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				openLoginDialog(); //再次弹出密码输入框
			}
		});

		//获取配置文件信息
		SharedPreferences sharedPreferences = getSharedPreferences(
				"LoginValue", Context.MODE_WORLD_READABLE);
		// getString()第二个参数为缺省值，如果preference中不存在该key，将返回缺省值
		password = sharedPreferences.getString("password", "");
		
		if (!password.equals("")) {
			// 弹出输入密码框
			openLoginDialog();
		}
	}

	/** 弹出输入密码框 **/
	private void openLoginDialog() {
		View view = getLayoutInflater().from(OpenEncyptActivity.this).inflate(
				R.layout.encrypt_dialog, null);
		final EditText editText = (EditText) view
				.findViewById(R.id.editText);
		
		cancelEncryptDlg = new Dialog(this, R.style.login_dialog_Dialog);
		cancelEncryptDlg.setContentView(view);
		
		Button sure_btn = (Button) view.findViewById(R.id.sure_btn);
		Button cancel_btn = (Button) view.findViewById(R.id.cancel_btn);
		sure_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
                String temPass = editText.getText().toString();
				
				if (password.equals(temPass)) {  
					//正确输入密码
					Intent intent  = new Intent(OpenEncyptActivity.this,MainActivity.class);
					intent.putExtra("open", "true");
					startActivity(intent);
					OpenEncyptActivity.this.finish();
				}else{
					cancelEncryptDlg.dismiss(); 
				}
		 }
		});
		//取消
		cancel_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				// 弹出Exit对话框
				cancelEncryptDlg.dismiss();
			}
		});
		cancelEncryptDlg.show();
	}

}
